package com.xiao.rpc.serializer;

import com.xiao.rpc.serializer.Serializers.JdkSerializer;
import com.xiao.rpc.spiload.SpiLoader;

/**
 * 序列化工厂
 *
 * 简化获取序列化器的对象的逻辑
 */
public class SerializerFactory {
    /**
     * 序列化反射(用于实现单例)
     *
     * 统一管理序列化器,以单例储存
     */
//    private static final Map<String, Serializer> KEYS_SERIALIZER_MAP = new HashMap<>(){{
//        put(SerializerConstant.JDK, new JdkSerializer());
//        put(SerializerConstant.JSON, new JsonSerializer());
//        put(SerializerConstant.KRYO, new KryoSerializer());
//        put(SerializerConstant.HESSIAN,new HessianSerializer());
//    }};

    static{
        SpiLoader.load(Serializer.class);
    }

    /**
     * 默认序列化器
     */
//    private static final Serializer DEFAULT_SERIALIZER = KEYS_SERIALIZER_MAP.getOrDefault(SerializerConstant.JDK, null);
    private static final Serializer DEFAULT_SERIALIZER = new JdkSerializer();

    /**
     * 获取序列化器
     *
     * @param key
     * @return
     */
    public static Serializer getInstance(String key) {
        return SpiLoader.getInstance(Serializer.class, key);
    }
}
